﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IP.Core;
using WebDS;
using WebDS.CDBNames;
using WebUS;
using IP.Core.IPCommon;

public partial class DanhMuc_F401_Quan_ly_bai_test_va_cau : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            load_data_2_grid_bai_test();
            load_data_2_grid_dm_cau();
            load_data_2_cbo_bai_test();
            m_hdf_trang_thai.Value = "0";
            m_hdf_trang_thai_bai.Value = "0";
        }
    }

    private void load_data_2_cbo_bai_test()
    {
        US_DM_BAI_TEST v_us_dm_bai_test = new US_DM_BAI_TEST();
        DS_DM_BAI_TEST v_ds_dm_bai_test = new DS_DM_BAI_TEST();

        v_us_dm_bai_test.FillDatasetAndSortBySTT(v_ds_dm_bai_test);
        m_cbo_bai_test.DataSource = v_ds_dm_bai_test.DM_BAI_TEST;
        m_cbo_bai_test.DataValueField = DM_BAI_TEST.ID;
        m_cbo_bai_test.DataTextField = DM_BAI_TEST.BAI_TEST;
        m_cbo_bai_test.DataBind();

        m_cbo_bai_test_tk.DataSource = v_ds_dm_bai_test.DM_BAI_TEST;
        m_cbo_bai_test_tk.DataValueField = DM_BAI_TEST.ID;
        m_cbo_bai_test_tk.DataTextField = DM_BAI_TEST.BAI_TEST;
        m_cbo_bai_test_tk.DataBind();
    }

    private void load_data_2_grid_dm_cau()
    {
        US_V_DM_CAU v_us_v_dm_cau = new US_V_DM_CAU();
        DS_V_DM_CAU v_ds_v_dm_cau = new DS_V_DM_CAU();

        v_us_v_dm_cau.FillDatasetSort(v_ds_v_dm_cau);
        m_grv_dm_cau.DataSource = v_ds_v_dm_cau.V_DM_CAU;
        m_grv_dm_cau.DataBind();
    }

    private void load_data_2_grid_bai_test()
    {
        US_DM_BAI_TEST v_us_dm_bai_test = new US_DM_BAI_TEST();
        DS_DM_BAI_TEST v_ds_dm_bai_test = new DS_DM_BAI_TEST();
        v_us_dm_bai_test.FillDatasetAndSortBySTT(v_ds_dm_bai_test);

        m_grv_dm_bai_test.DataSource = v_ds_dm_bai_test.DM_BAI_TEST;
        m_grv_dm_bai_test.DataBind();

    }
    protected void m_grv_dm_bai_test_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            m_lbl_mess.Text = "";
            delete_dm_bai_test(e.RowIndex);
        }
        catch (Exception v_e)
        {
            // de su dung CsystemLog_301 bat buoc Site phai dat trong thu muc cap 1. Vi du: DanhMuc/Dictionary.aspx
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }

    private void delete_dm_bai_test(int i_int_row_index)
    {
        try
        {
            US_DM_BAI_TEST v_us_dm_bai_test = new US_DM_BAI_TEST();
            decimal v_dc_id_dm_tu_dien = CIPConvert.ToDecimal(m_grv_dm_bai_test.DataKeys[i_int_row_index].Value);
            v_us_dm_bai_test.DeleteByID(v_dc_id_dm_tu_dien);
            load_data_2_grid_bai_test();
            m_lbl_mess.Text = "Xóa bản ghi thành công.";
        }
        catch (Exception v_e)
        {
            m_lbl_mess.Text = "Lỗi trong quá trình xóa bản ghi.";
            throw v_e;
        }
    }
    protected void m_grv_dm_bai_test_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        try
        {
            m_hdf_trang_thai_bai.Value = "1";
            m_lbl_mess.Text = "";
            load_update_dm_bai_test(e.NewSelectedIndex);
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }

    private void load_update_dm_bai_test(int i_int_row_index)
    {
        try
        {
            decimal v_dc_id_dm_bai_test = CIPConvert.ToDecimal(m_grv_dm_bai_test.DataKeys[i_int_row_index].Value);
            US_DM_BAI_TEST v_us_dm_bai_test = new US_DM_BAI_TEST(v_dc_id_dm_bai_test);
            m_hdf_id_dm_bai_test.Value = CIPConvert.ToStr(v_dc_id_dm_bai_test);
            us_object_2_form(v_us_dm_bai_test);
        }
        catch (Exception v_e)
        {
            m_lbl_mess.Text = "Lỗi trong quá trình xóa bản ghi.";
            throw v_e;
        }
    }

    private void us_object_2_form(US_DM_BAI_TEST v_us_dm_bai_test)
    {
        m_txt_ten_bai_test.Text = v_us_dm_bai_test.strBAI_TEST;
        m_txt_stt.Text = v_us_dm_bai_test.dcSTT.ToString();
    }


    protected void m_cmd_tao_moi_Click(object sender, EventArgs e)
    {
        try
        {
            if (m_hdf_trang_thai_bai.Value == "0")
            {
                US_DM_BAI_TEST v_us_dm_bai_test = new US_DM_BAI_TEST();
                m_lbl_mess.Text = "";
                insert_dm_bai_test(v_us_dm_bai_test);
            }
            else
            {
                m_lbl_mess.Text = "";
                update_dm_bai_test();
            }
            load_data_2_cbo_bai_test();
            m_hdf_trang_thai_bai.Value = "0";
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }

    private void insert_dm_bai_test(US_DM_BAI_TEST v_us_dm_bai_test)
    {
        try
        {
            m_lbl_mess.Text = "";
            m_grv_dm_bai_test.EditIndex = -1;
            if (Page.IsValid)
            {
                if (!check_validate_is_ok()) return;

                form_2_us_object(v_us_dm_bai_test);
                v_us_dm_bai_test.Insert();

                reset_control_in_form();
                m_lbl_mess.Text = "Đã thêm mới thành công!";
                load_data_2_grid_bai_test();
            }
        }
        catch (Exception v_e)
        {
            m_lbl_mess.Text = "Lỗi trong quá trình thêm mới bản ghi!";
            throw v_e;
        }
    }

    private void form_2_us_object(US_DM_BAI_TEST i_us_dm_bai_test)
    {
        i_us_dm_bai_test.strBAI_TEST = m_txt_ten_bai_test.Text.TrimEnd();
        i_us_dm_bai_test.dcSTT =CIPConvert.ToDecimal(m_txt_stt.Text.TrimEnd());
    }

    private void reset_control_in_form()
    {
        m_lbl_mess.Text = "";
        m_lbl_thong_bao.Text = "";
        m_txt_stt.Text = "";
        m_txt_ten_bai_test.Text = "";
    }

    private bool check_validate_is_ok()
    {
        return true;
    }

    private void update_dm_bai_test()
    {
        try
        {
            m_lbl_mess.Text = "";
            m_grv_dm_bai_test.EditIndex = -1;
            if (Page.IsValid)
            {

                if (!check_validate_is_ok()) return;
                US_DM_BAI_TEST v_us_dm_bai_test = new US_DM_BAI_TEST();
                form_2_us_object(v_us_dm_bai_test);
                v_us_dm_bai_test.dcID = CIPConvert.ToDecimal(this.m_hdf_id_dm_bai_test.Value);
                v_us_dm_bai_test.Update();

                reset_control_in_form();
                m_lbl_mess.Text = "Đã cập nhật bản ghi thành công!";
                m_grv_dm_bai_test.EditIndex = -1;
                load_data_2_grid_bai_test();
            }
        }
        catch (Exception v_e)
        {
            m_lbl_mess.Text = "Lỗi trong quá trình cập nhật bản ghi!";
            throw v_e;
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        reset_control_in_form();
    }
    protected void m_cmd_insert_Click(object sender, EventArgs e)
    {
        try
        {
            if (m_hdf_trang_thai.Value == "0")
            {
                US_DM_CAU v_us_dm_cau = new US_DM_CAU();
                m_lbl_mess02.Text = "";
                insert_dm_cau(v_us_dm_cau);
            }
            else
            {
                update_dm_cau();
            }
            m_hdf_trang_thai.Value = "0";
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }

    private void insert_dm_cau(US_DM_CAU v_us_dm_cau)
    {
        try
        {
            m_lbl_mess02.Text = "";
            m_grv_dm_cau.EditIndex = -1;
            if (Page.IsValid)
            {
                if (!check_validate_is_ok()) return;

                form_2_us_object(v_us_dm_cau);
                v_us_dm_cau.Insert();

                reset_control_in_form_cau();
                m_lbl_mess02.Text = "Đã thêm mới thành công!";
                load_data_2_grid_dm_cau();
            }
        }
        catch (Exception v_e)
        {
            m_lbl_mess.Text = "Lỗi trong quá trình thêm mới bản ghi!";
            throw v_e;
        }
    }

    private void form_2_us_object(US_DM_CAU v_us_dm_cau)
    {
        v_us_dm_cau.dcID_BAI_TEST = CIPConvert.ToDecimal(m_cbo_bai_test.SelectedValue);
        v_us_dm_cau.dcSTT = CIPConvert.ToDecimal(m_txt_stt_cau.Text);
        v_us_dm_cau.strCHU_THICH_CHUNG = delTagP(m_cke_giai_thich.Text);
        v_us_dm_cau.strDICH_NGHIA = delTagP(m_cke_dich_nghia.Text);
        v_us_dm_cau.strNOI_DUNG_CAU = delTagP(m_cke_noi_dung.Text);
    }

    private string delTagP(string ip_str) 
    {
        string s = ip_str;
        s = s.Replace("<p>", " ");
        s = s.Replace("</p>", " ");
        s.Trim();
        return s;
    }

    private void reset_control_in_form_cau()
    {
        m_txt_stt_cau.Text = "";
        m_cke_dich_nghia.Text = "";
        m_cke_giai_thich.Text = "";
        m_cke_noi_dung.Text = "";
    }
    protected void m_cmd_clear_Click(object sender, EventArgs e)
    {
        reset_control_in_form_cau();
    }
    protected void m_grv_dm_cau_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            m_lbl_mess02.Text = "";
            delete_dm_cau(e.RowIndex);
        }
        catch (Exception v_e)
        {
            // de su dung CsystemLog_301 bat buoc Site phai dat trong thu muc cap 1. Vi du: DanhMuc/Dictionary.aspx
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }

    private void delete_dm_cau(int i_int_row_index)
    {
        try
        {
            US_DM_CAU v_us_dm_cau = new US_DM_CAU();
            decimal v_dc_id_dm_cau = CIPConvert.ToDecimal(m_grv_dm_cau.DataKeys[i_int_row_index].Value);
            v_us_dm_cau.DeleteByID(v_dc_id_dm_cau);
            load_data_2_grid_dm_cau();
            m_lbl_mess02.Text = "Xóa bản ghi thành công.";
        }
        catch (Exception v_e)
        {
            m_lbl_mess.Text = "Lỗi trong quá trình xóa bản ghi.";
            throw v_e;
        }
    }
    protected void m_grv_dm_cau_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        try
        {
            m_hdf_trang_thai.Value = "1";
            m_lbl_mess02.Text = "";
            load_update_dm_cau(e.NewSelectedIndex);
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }

    private void load_update_dm_cau(int i_int_row_index)
    {
        try
        {
            decimal v_dc_id_dm_cau = CIPConvert.ToDecimal(m_grv_dm_cau.DataKeys[i_int_row_index].Value);
            US_DM_CAU v_us_dm_cau = new US_DM_CAU(v_dc_id_dm_cau);
            m_hdf_id_dm_cau.Value = CIPConvert.ToStr(v_dc_id_dm_cau);
            us_object_2_form(v_us_dm_cau);
        }
        catch (Exception v_e)
        {
            m_lbl_mess.Text = "Lỗi trong quá trình xóa bản ghi.";
            throw v_e;
        }
    }

    private void us_object_2_form(US_DM_CAU v_us_dm_cau)
    {
        m_cbo_bai_test.SelectedValue =CIPConvert.ToStr(v_us_dm_cau.dcID_BAI_TEST);
        m_txt_stt_cau.Text = v_us_dm_cau.dcSTT.ToString();
        m_cke_dich_nghia.Text = v_us_dm_cau.strDICH_NGHIA;
        m_cke_giai_thich.Text = v_us_dm_cau.strCHU_THICH_CHUNG;
        m_cke_noi_dung.Text = v_us_dm_cau.strNOI_DUNG_CAU;
    }
    protected void m_cmd_update_Click(object sender, EventArgs e)
    {
        try
        {
            m_lbl_mess02.Text = "";
            update_dm_cau();
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }

    private void update_dm_cau()
    {
        try
        {
            m_lbl_mess02.Text = "";
            m_grv_dm_cau.EditIndex = -1;
            if (Page.IsValid)
            {

                if (!check_validate_is_ok()) return;
                US_DM_CAU v_us_dm_cau = new US_DM_CAU();
                form_2_us_object(v_us_dm_cau);
                v_us_dm_cau.dcID = CIPConvert.ToDecimal(this.m_hdf_id_dm_cau.Value);
                v_us_dm_cau.Update();

                reset_control_in_form_cau();
                m_lbl_mess02.Text = "Đã cập nhật bản ghi thành công!";
                m_grv_dm_cau.EditIndex = -1;
                load_data_2_grid_dm_cau();
            }
        }
        catch (Exception v_e)
        {
            m_lbl_mess.Text = "Lỗi trong quá trình cập nhật bản ghi!";
            throw v_e;
        }
    }
    protected void m_grv_dm_cau_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            m_grv_dm_cau.PageIndex = e.NewPageIndex;
            load_data_2_grid_dm_cau();
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_cmd_search_Click(object sender, EventArgs e)
    {
        US_V_DM_CAU v_us_v_dm_cau = new US_V_DM_CAU();
        DS_V_DM_CAU v_ds_v_dm_cau = new DS_V_DM_CAU();

        string str_bai_test = m_cbo_bai_test_tk.SelectedItem.Text;
        decimal stt_cau = CIPConvert.ToDecimal(m_txt_stt_cau_tk.Text);

        v_us_v_dm_cau.FillDatasetSortAndSearch(v_ds_v_dm_cau,str_bai_test,stt_cau);
        m_grv_dm_cau.DataSource = v_ds_v_dm_cau.V_DM_CAU;
        m_grv_dm_cau.DataBind();
    }
    protected void m_grv_dm_bai_test_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            m_grv_dm_bai_test.PageIndex = e.NewPageIndex;
            load_data_2_grid_bai_test();
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
}